import Vue from 'vue';
import ajax from '../js_sdk/u-ajax/uni-ajax';
import {baseUrl} from "@/common/Api";

const _ajax = ajax.create({
	// Default configuration
	// baseURL: 'http://49.235.91.253/api',
	baseURL:baseUrl
	// baseURL: 'http://192.168.0.22:8011',
});

_ajax.interceptors.request.use(
	config => {
		if(uni.getStorageSync('token')){
			config.header = {
				ssid: uni.getStorageSync('token'),
				// ssid:'ADMIN_AUTH_CHECK'
			}
		}
		return config;
	},
	error => {
		// Do something with request error
		return error;
	}
);

_ajax.interceptors.response.use(
	response => {
		// Do something with response data
		if(response.data.state == 201){
			uni.navigateTo({
				url:'/pages/login/login'
			})
		}else if(response.data.state == 403){
			uni.showToast({
			    title: '您没有该权限',
			    duration: 2000
			});
		}
		return response;
	},
	error => {
		// Do something with response error
		return error;
	}
);

Vue.prototype.$ajax = _ajax;

export default _ajax;
